package com.example.lenovo.weiyi;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.demon.XQ;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

public class HealthMessXQActivity extends BaseActivity {

    @BindView(R.id.tv_healthy_title)
    TextView tvHealthyTitle;
    @BindView(R.id.tv_healthy_time)
    TextView tvHealthyTime;
    @BindView(R.id.web_healthy)
    WebView webHealthy;
    private String Is_collection;
    private ImageView imvcollection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_mess_xq);
        ButterKnife.bind(this);
        ShowLayCollection();
        ChangLayTitle("运动健康知识");
        LayBack();
        init();
        getData();
    }

    public void init() {
        //收藏
        imvcollection = (ImageView) findViewById(R.id.imv_collection);

    }

    /**
     * 详情
     */
    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.XQ, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(HealthMessXQActivity.this, "uid"));
        mRequest.add("courseId", getIntent().getStringExtra("id"));
        CallServer.getRequestInstance().add(HealthMessXQActivity.this, 0, mRequest,
                new CustomHttpListener(HealthMessXQActivity.this, true, XQ.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        XQ model = ((XQ) data);
                        Is_collection = model.getObject().getIfCollect() + "";
                        if (("1").equals(Is_collection)) {
                            imvcollection.setBackgroundResource(R.drawable.collection_pre_b);
                        } else {
                            imvcollection.setBackgroundResource(R.drawable.collection_b);
                        }
                        tvHealthyTitle.setText(model.getObject().getTitle());
                        tvHealthyTime.setText(model.getObject().getCreateDate());
                        String str_canshu = "<!doctype html><html>\n<meta charset=\"utf-8\"><style type=\"text/css\">body{padding:0; margin:0;}\n." +
                                "view_h1{ width:90%%;display:block; "
                                + "overflow:hidden; margin:0 auto; font-size:1.2em; color:#333; padding:1em 0;"
                                + " line-height:1.2em; text-align:center;}\n.view_time{ width:90%; display:block; "
                                + "text-align:center;overflow:hidden; margin:0 auto; font-size:1em; color:#999;}\n.con{width:90%; "
                                + "margin:0 auto; color:#fff; color:#333; padding:0.5em 0; overflow:hidden; display:block; font-size:0.92em;"
                                + " line-height:1.8em;}\n.con h1,h2,h3,h4,h5,h6{ font-size:1em;}\n .con img{width: auto; max-width: 100%;"
                                + "height: auto;margin:0 auto;}\n</style>\n<body style=\"padding:0; margin:0; \"><div class=\"con\">"
                                + model.getObject().getIntro() + "</div></body></html>";
                        webHealthy.getSettings().setJavaScriptEnabled(true); //设置支持Javascript
                        webHealthy.loadDataWithBaseURL(HttpIP.Base_IpIMage
                                , str_canshu, "text/html", "UTF-8", null);
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if(!TextUtils.isEmpty(obj.getString("msg"))){
                            CommonUtil.showToask(HealthMessXQActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, false);
    }

    /**
     * 论坛收藏
     */
    private void getCollectionData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.Collection, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(HealthMessXQActivity.this, "uid"));
        mRequest.add("courseId", getIntent().getStringExtra("id"));
        CallServer.getRequestInstance().add(HealthMessXQActivity.this, 0, mRequest,
                new CustomHttpListener(HealthMessXQActivity.this, true, CommonM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        Is_collection = "1";
                        imvcollection.setBackgroundResource(R.drawable.collection_pre_b);
                        Params.Change_Collection = "1";
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(HealthMessXQActivity.this, obj.getString("msg"));
                    }
                }, true, false);
    }

    /**
     * 论坛取消收藏
     */
    private void getCancellCollectionData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.CancelCollection, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(HealthMessXQActivity.this, "uid"));
        mRequest.add("courseId", getIntent().getStringExtra("id"));//取消论坛时   传questionID  取消知识是  传CourseID
        CallServer.getRequestInstance().add(HealthMessXQActivity.this, 0, mRequest,
                new CustomHttpListener(HealthMessXQActivity.this, true, CommonM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        Is_collection = "0";
                        imvcollection.setBackgroundResource(R.drawable.collection_b);
                        Params.Change_Collection = "1";
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(HealthMessXQActivity.this, obj.getString("msg"));
                    }
                }, true, false);
    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.imv_collection://收藏
                if (("1").equals(Is_collection)) {
                    getCancellCollectionData();
                } else {
                    getCollectionData();
                }
                break;
        }
    }
}
